package cn.it.xiaohan.note.web;

import cn.it.xiaohan.note.po.NoteType;
import cn.it.xiaohan.note.po.User;
import cn.it.xiaohan.note.service.NoteTypeService;
import cn.it.xiaohan.note.util.JsonUtil;
import cn.it.xiaohan.note.vo.ResultInfo;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/type")
public class NoteTypeServlet extends HttpServlet {

    private NoteTypeService typeService = new NoteTypeService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // 设置导航栏高亮
        request.setAttribute("menu_page","type");

        // 得到用户行为
        String actionName = request.getParameter("actionName");

        // 判断用户行为
        if ("list".equals(actionName)){

            // 查询云记类型列表
            typeList(request,response);
        } else if ("delete".equals(actionName)){

            // 删除云记类型
            deleteType(request,response);
        } else if ("addOrUpdate".equals(actionName)){

            // 添加或者修改操作
            addOrUpdateType(request,response);
        }
    }

    private void addOrUpdateType(HttpServletRequest request, HttpServletResponse response) {
        // 接收参数
        String typeName = request.getParameter("typeName");
        String typeId = request.getParameter("typeId");
        // 从session中的到用户信息
        User user = (User) request.getSession().getAttribute("user");
        // 调用service方法返回resultInfo对象
        ResultInfo<Integer> resultInfo = typeService.addOrUpdateType(typeId,typeName,user.getUserId());
        // 将得到的resultInfo对象，转换成Json格式的字符串，响应给前台的Ajax，接收
        JsonUtil.toJson(response,resultInfo);
    }

    /**
     * 删除云记类型
     * @param request
     * @param response
     */
    private void deleteType(HttpServletRequest request, HttpServletResponse response) {
        // 获取参数的到类型ID
        String typeId = request.getParameter("typeId");
        // 调用service层方法，返回resultInfo对象
        ResultInfo<NoteType> resultInfo = typeService.deleteType(typeId);
        // 将resultInfo转换成Json格式的字符串，响应给ajax的回调函数
        JsonUtil.toJson(response,resultInfo);

    }

    /**
     * 查询云记类型列表
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void typeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取session域中的用户对象
        User user = (User) request.getSession().getAttribute("user");
        // 调用service层的方法，返回类型集合
        List<NoteType> typeList = typeService.findTypeByUserId(user.getUserId());
        // 将查询到的typeList设置到请求域中
        request.setAttribute("typeList",typeList);
        // 动态加载云记类型列表页面
        request.setAttribute("changePage","type/list.jsp");
        // 请求转发跳转到个人中心（index.jsp）
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
}
